;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019 Kei Kebreau <kkebreau@posteo.net>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (switch-homebrew)
  #:use-module (guix packages)
  #:use-module (gnu packages)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module (guix utils)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system trivial)
  #:use-module (gnu build cross-toolchain)
  #:use-module (gnu packages cross-base)
  #:use-module (gnu packages embedded)
  #:use-module (gnu packages libusb)
  #:use-module (gnu packages python))

(define-public fusee-launcher
  (package
    (name "fusee-launcher")
    (version "1.0")
    (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/Qyriad/fusee-launcher.git")
                    (commit version)))
              (file-name (git-file-name name version))
              (sha256
               (base32
                "1pqkgw5bk0xcz9x7pc1f0r0b9nsc8jnnvcs1315d8ml8mx23fshm"))))
    (build-system gnu-build-system)
    (arguments
     `(#:tests? #f ; no check target
       #:phases
       (modify-phases %standard-phases
         (replace 'configure
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (share (string-append out "/share/fusee-launcher")))
               ;; Patch the path to the intermezzo relocator binary.
               (substitute* "fusee-launcher.py"
                 (("os\\.path\\.dirname\\(os\\.path\\.abspath\\(__file__\\)\\)")
                  (string-append "'" share "'")))
               #t)))
         (replace 'install
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (bin (string-append out "/bin"))
                    (share (string-append out "/share/fusee-launcher")))
               (install-file "fusee-launcher.py" bin)
               (install-file "intermezzo.bin" share)
               #t)))
         (add-after 'install 'wrap
           (lambda* (#:key inputs outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out"))
                    (pyusb (assoc-ref inputs "python-pyusb"))
                    (sitedir
                     (lambda (package)
                       (string-append package
                                      "/lib/python"
                                      ,(version-major+minor
                                        (package-version python))
                                      "/site-packages")))
                    (PYTHONPATH (sitedir pyusb)))
               (wrap-program (string-append out "/bin/fusee-launcher.py")
                 `("PYTHONPATH" ":" prefix (,PYTHONPATH)))
               #t))))))
    (native-inputs
     `(("xbinutils" ,(cross-binutils "arm-none-eabi"))
       ("xgcc" ,gcc-arm-none-eabi-6)))
    (inputs
     `(("python" ,python)
       ("python-pyusb" ,python-pyusb)))
    (home-page "https://github.com/Qyriad/fusee-launcher")
    (synopsis "Launcher for one of the Tegra X1 bootROM exploits")
    (description
     "fusee-launcher is a launcher for Fusée Gelée, a coldboot vulnerability
that allows full, unauthenticated arbitrary code execution from an early bootROM
context via Tegra Recovery Mode (RCM) on NVIDIA's Tegra line of embedded
processors.")
    (license license:gpl2)))
